const express = require('express');
const axios = require('axios');
const bodyParser = require('body-parser');

const app = express();
const port = 3000;  // 修改为3000端口

app.use(bodyParser.json());

app.post('/gpt', async (req, res) => {
  const { messages, model } = req.body;

  try {
    const response = await axios.post('https://yewu.bcwhkj.cn/api/v2.Gptliu/search', {
      messages: messages,
      model: model,
      stream: true // 设置为流式
    }, {
      headers: {
        'Authorization': `Bearer 3GcAy4Ba5pTqaIYRz0kn5VkouahxFAzB`,
        'Content-Type': 'application/json'
      },
      responseType: 'stream' // 设置响应类型为流
    });

    response.data.on('data', chunk => {
      res.write(chunk.toString());
    });

    response.data.on('end', () => {
      res.end();
    });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
